﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using XYW.DBUtility;//Please add references
namespace XYW.DAL
{
	/// <summary>
	/// 数据访问类:Users
	/// </summary>
	public partial class Users
	{
		public Users()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "Users"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Users");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XYW.Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Users(");
			strSql.Append("name,sex,number,passWord,classId,avatar,nickName,signature,email,QQ,cellphone,address,postId,workDanwei,workZhiwei,workZhichen,rongYu,isSuper)");
			strSql.Append(" values (");
			strSql.Append("@name,@sex,@number,@passWord,@classId,@avatar,@nickName,@signature,@email,@QQ,@cellphone,@address,@postId,@workDanwei,@workZhiwei,@workZhichen,@rongYu,@isSuper)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@sex", SqlDbType.VarChar,50),
					new SqlParameter("@number", SqlDbType.VarChar,50),
					new SqlParameter("@passWord", SqlDbType.VarChar,200),
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@avatar", SqlDbType.VarChar,150),
					new SqlParameter("@nickName", SqlDbType.VarChar,50),
					new SqlParameter("@signature", SqlDbType.VarChar,280),
					new SqlParameter("@email", SqlDbType.VarChar,100),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@cellphone", SqlDbType.VarChar,50),
					new SqlParameter("@address", SqlDbType.VarChar,50),
					new SqlParameter("@postId", SqlDbType.VarChar,50),
					new SqlParameter("@workDanwei", SqlDbType.VarChar,150),
					new SqlParameter("@workZhiwei", SqlDbType.VarChar,50),
					new SqlParameter("@workZhichen", SqlDbType.VarChar,50),
					new SqlParameter("@rongYu", SqlDbType.VarChar,550),
					new SqlParameter("@isSuper", SqlDbType.Int,4)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.sex;
			parameters[2].Value = model.number;
			parameters[3].Value = model.passWord;
			parameters[4].Value = model.classId;
			parameters[5].Value = model.avatar;
			parameters[6].Value = model.nickName;
			parameters[7].Value = model.signature;
			parameters[8].Value = model.email;
			parameters[9].Value = model.QQ;
			parameters[10].Value = model.cellphone;
			parameters[11].Value = model.address;
			parameters[12].Value = model.postId;
			parameters[13].Value = model.workDanwei;
			parameters[14].Value = model.workZhiwei;
			parameters[15].Value = model.workZhichen;
			parameters[16].Value = model.rongYu;
			parameters[17].Value = model.isSuper;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XYW.Model.Users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Users set ");
			strSql.Append("name=@name,");
			strSql.Append("sex=@sex,");
			strSql.Append("number=@number,");
			strSql.Append("passWord=@passWord,");
			strSql.Append("classId=@classId,");
			strSql.Append("avatar=@avatar,");
			strSql.Append("nickName=@nickName,");
			strSql.Append("signature=@signature,");
			strSql.Append("email=@email,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("cellphone=@cellphone,");
			strSql.Append("address=@address,");
			strSql.Append("postId=@postId,");
			strSql.Append("workDanwei=@workDanwei,");
			strSql.Append("workZhiwei=@workZhiwei,");
			strSql.Append("workZhichen=@workZhichen,");
			strSql.Append("rongYu=@rongYu,");
			strSql.Append("isSuper=@isSuper");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@sex", SqlDbType.VarChar,50),
					new SqlParameter("@number", SqlDbType.VarChar,50),
					new SqlParameter("@passWord", SqlDbType.VarChar,200),
					new SqlParameter("@classId", SqlDbType.Int,4),
					new SqlParameter("@avatar", SqlDbType.VarChar,150),
					new SqlParameter("@nickName", SqlDbType.VarChar,50),
					new SqlParameter("@signature", SqlDbType.VarChar,280),
					new SqlParameter("@email", SqlDbType.VarChar,100),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@cellphone", SqlDbType.VarChar,50),
					new SqlParameter("@address", SqlDbType.VarChar,50),
					new SqlParameter("@postId", SqlDbType.VarChar,50),
					new SqlParameter("@workDanwei", SqlDbType.VarChar,150),
					new SqlParameter("@workZhiwei", SqlDbType.VarChar,50),
					new SqlParameter("@workZhichen", SqlDbType.VarChar,50),
					new SqlParameter("@rongYu", SqlDbType.VarChar,550),
					new SqlParameter("@isSuper", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.sex;
			parameters[2].Value = model.number;
			parameters[3].Value = model.passWord;
			parameters[4].Value = model.classId;
			parameters[5].Value = model.avatar;
			parameters[6].Value = model.nickName;
			parameters[7].Value = model.signature;
			parameters[8].Value = model.email;
			parameters[9].Value = model.QQ;
			parameters[10].Value = model.cellphone;
			parameters[11].Value = model.address;
			parameters[12].Value = model.postId;
			parameters[13].Value = model.workDanwei;
			parameters[14].Value = model.workZhiwei;
			parameters[15].Value = model.workZhichen;
			parameters[16].Value = model.rongYu;
			parameters[17].Value = model.isSuper;
			parameters[18].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Users ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XYW.Model.Users GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,name,sex,number,passWord,classId,avatar,nickName,signature,email,QQ,cellphone,address,postId,workDanwei,workZhiwei,workZhichen,rongYu,isSuper from Users ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			XYW.Model.Users model=new XYW.Model.Users();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sex"]!=null && ds.Tables[0].Rows[0]["sex"].ToString()!="")
				{
					model.sex=ds.Tables[0].Rows[0]["sex"].ToString();
				}
				if(ds.Tables[0].Rows[0]["number"]!=null && ds.Tables[0].Rows[0]["number"].ToString()!="")
				{
					model.number=ds.Tables[0].Rows[0]["number"].ToString();
				}
				if(ds.Tables[0].Rows[0]["passWord"]!=null && ds.Tables[0].Rows[0]["passWord"].ToString()!="")
				{
					model.passWord=ds.Tables[0].Rows[0]["passWord"].ToString();
				}
				if(ds.Tables[0].Rows[0]["classId"]!=null && ds.Tables[0].Rows[0]["classId"].ToString()!="")
				{
					model.classId=int.Parse(ds.Tables[0].Rows[0]["classId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["avatar"]!=null && ds.Tables[0].Rows[0]["avatar"].ToString()!="")
				{
					model.avatar=ds.Tables[0].Rows[0]["avatar"].ToString();
				}
				if(ds.Tables[0].Rows[0]["nickName"]!=null && ds.Tables[0].Rows[0]["nickName"].ToString()!="")
				{
					model.nickName=ds.Tables[0].Rows[0]["nickName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["signature"]!=null && ds.Tables[0].Rows[0]["signature"].ToString()!="")
				{
					model.signature=ds.Tables[0].Rows[0]["signature"].ToString();
				}
				if(ds.Tables[0].Rows[0]["email"]!=null && ds.Tables[0].Rows[0]["email"].ToString()!="")
				{
					model.email=ds.Tables[0].Rows[0]["email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["cellphone"]!=null && ds.Tables[0].Rows[0]["cellphone"].ToString()!="")
				{
					model.cellphone=ds.Tables[0].Rows[0]["cellphone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["address"]!=null && ds.Tables[0].Rows[0]["address"].ToString()!="")
				{
					model.address=ds.Tables[0].Rows[0]["address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["postId"]!=null && ds.Tables[0].Rows[0]["postId"].ToString()!="")
				{
					model.postId=ds.Tables[0].Rows[0]["postId"].ToString();
				}
				if(ds.Tables[0].Rows[0]["workDanwei"]!=null && ds.Tables[0].Rows[0]["workDanwei"].ToString()!="")
				{
					model.workDanwei=ds.Tables[0].Rows[0]["workDanwei"].ToString();
				}
				if(ds.Tables[0].Rows[0]["workZhiwei"]!=null && ds.Tables[0].Rows[0]["workZhiwei"].ToString()!="")
				{
					model.workZhiwei=ds.Tables[0].Rows[0]["workZhiwei"].ToString();
				}
				if(ds.Tables[0].Rows[0]["workZhichen"]!=null && ds.Tables[0].Rows[0]["workZhichen"].ToString()!="")
				{
					model.workZhichen=ds.Tables[0].Rows[0]["workZhichen"].ToString();
				}
				if(ds.Tables[0].Rows[0]["rongYu"]!=null && ds.Tables[0].Rows[0]["rongYu"].ToString()!="")
				{
					model.rongYu=ds.Tables[0].Rows[0]["rongYu"].ToString();
				}
				if(ds.Tables[0].Rows[0]["isSuper"]!=null && ds.Tables[0].Rows[0]["isSuper"].ToString()!="")
				{
					model.isSuper=int.Parse(ds.Tables[0].Rows[0]["isSuper"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}
        public XYW.Model.Users GetModel(string userName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,name,sex,number,passWord,classId,avatar,nickName,signature,email,QQ,cellphone,address,postId,workDanwei,workZhiwei,workZhichen,rongYu,isSuper from Users ");
            strSql.Append(" where name=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.VarChar,400)
			};
            parameters[0].Value = userName;

            XYW.Model.Users model = new XYW.Model.Users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sex"] != null && ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["number"] != null && ds.Tables[0].Rows[0]["number"].ToString() != "")
                {
                    model.number = ds.Tables[0].Rows[0]["number"].ToString();
                }
                if (ds.Tables[0].Rows[0]["passWord"] != null && ds.Tables[0].Rows[0]["passWord"].ToString() != "")
                {
                    model.passWord = ds.Tables[0].Rows[0]["passWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["classId"] != null && ds.Tables[0].Rows[0]["classId"].ToString() != "")
                {
                    model.classId = int.Parse(ds.Tables[0].Rows[0]["classId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["avatar"] != null && ds.Tables[0].Rows[0]["avatar"].ToString() != "")
                {
                    model.avatar = ds.Tables[0].Rows[0]["avatar"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nickName"] != null && ds.Tables[0].Rows[0]["nickName"].ToString() != "")
                {
                    model.nickName = ds.Tables[0].Rows[0]["nickName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["signature"] != null && ds.Tables[0].Rows[0]["signature"].ToString() != "")
                {
                    model.signature = ds.Tables[0].Rows[0]["signature"].ToString();
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    model.email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QQ"] != null && ds.Tables[0].Rows[0]["QQ"].ToString() != "")
                {
                    model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["cellphone"] != null && ds.Tables[0].Rows[0]["cellphone"].ToString() != "")
                {
                    model.cellphone = ds.Tables[0].Rows[0]["cellphone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["address"] != null && ds.Tables[0].Rows[0]["address"].ToString() != "")
                {
                    model.address = ds.Tables[0].Rows[0]["address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["postId"] != null && ds.Tables[0].Rows[0]["postId"].ToString() != "")
                {
                    model.postId = ds.Tables[0].Rows[0]["postId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["workDanwei"] != null && ds.Tables[0].Rows[0]["workDanwei"].ToString() != "")
                {
                    model.workDanwei = ds.Tables[0].Rows[0]["workDanwei"].ToString();
                }
                if (ds.Tables[0].Rows[0]["workZhiwei"] != null && ds.Tables[0].Rows[0]["workZhiwei"].ToString() != "")
                {
                    model.workZhiwei = ds.Tables[0].Rows[0]["workZhiwei"].ToString();
                }
                if (ds.Tables[0].Rows[0]["workZhichen"] != null && ds.Tables[0].Rows[0]["workZhichen"].ToString() != "")
                {
                    model.workZhichen = ds.Tables[0].Rows[0]["workZhichen"].ToString();
                }
                if (ds.Tables[0].Rows[0]["rongYu"] != null && ds.Tables[0].Rows[0]["rongYu"].ToString() != "")
                {
                    model.rongYu = ds.Tables[0].Rows[0]["rongYu"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isSuper"] != null && ds.Tables[0].Rows[0]["isSuper"].ToString() != "")
                {
                    model.isSuper = int.Parse(ds.Tables[0].Rows[0]["isSuper"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,sex,number,passWord,classId,avatar,nickName,signature,email,QQ,cellphone,address,postId,workDanwei,workZhiwei,workZhichen,rongYu,isSuper ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,name,sex,number,passWord,classId,avatar,nickName,signature,email,QQ,cellphone,address,postId,workDanwei,workZhiwei,workZhichen,rongYu,isSuper ");
			strSql.Append(" FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from Users T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Users";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

